package com.glsc.ngateway.opmanage.controller;

import com.glsc.ngateway.opmanage.dto.RulefilelDto;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.*;

import com.glsc.ngateway.common.api.common.enums.PageConstant;
import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.rule.RuleFileInfo;
import com.glsc.ngateway.common.base.dto.opmanage.report.DerivativeResponse;
import com.glsc.ngateway.opmanage.service.rule.RuleService;
import com.glsc.ngateway.opmanage.utils.SpecificationFactory;

import io.swagger.annotations.ApiParam;

/**
 * @author houzhc
 * @description: TODO hzc
 * @date 2024年08月06日
 */
@RestController
@RequestMapping("/api/rules")
public class RulesController {

    @Autowired
    private RuleService ruleService;

    @PostMapping("/upload")
    public DerivativeResponse<?> upload(@RequestBody RuleFileInfo ruleFileInfo) {
        if (ruleFileInfo == null) {
            return DerivativeResponse.error("数据为空", null);
        }
        if (StringUtils.isEmpty(ruleFileInfo.getDocId())) {
            return DerivativeResponse.error("文件Id为空", null);
        }

        ruleService.save(ruleFileInfo);
        return DerivativeResponse.success("上传成功", null);
    }

    @RequestMapping("/delete")
    public DerivativeResponse<?> delete(@RequestParam("id") Integer id) {
        if (id == null) {
            return DerivativeResponse.error("Id为空", null);
        }
        ruleService.deleteById(id);
        return DerivativeResponse.success("删除成功", null);
    }

    @RequestMapping("/update")
    public DerivativeResponse<?> update(@RequestBody RuleFileInfo ruleFileInfo) {
        if (ruleFileInfo == null) {
            return DerivativeResponse.error("数据为空", null);
        }
        if (ruleFileInfo.getId() == null) {
            return DerivativeResponse.error("Id为空", null);
        }
        if (StringUtils.isEmpty(ruleFileInfo.getDocId())) {
            return DerivativeResponse.error("文件Id为空", null);
        }

        ruleService.save(ruleFileInfo);
        return DerivativeResponse.success("更新成功", null);
    }

    @PostMapping("/listpage")
    public PlatformResponse<Page<RuleFileInfo>> page(@RequestBody RulefilelDto rulefilelDto) {
        if (rulefilelDto.getPageNo() == null) {
            rulefilelDto.setPageNo(1);
        }
        if (rulefilelDto.getPageSize() == null) {
            rulefilelDto.setPageSize(PageConstant.PageSize);
        }
        Specification spec = null;
        SpecificationFactory.SpecElement typeE = new SpecificationFactory.SpecElement(SpecificationFactory.Type.EQ, "type", rulefilelDto.getType());
        if (rulefilelDto.getFileName() != null) {
            SpecificationFactory.SpecElement fileNameE = new SpecificationFactory.SpecElement(SpecificationFactory.Type.LIKE, "fileName", rulefilelDto.getFileName());
            spec = SpecificationFactory.build(SpecificationFactory.OP.AND, fileNameE, typeE);
        } else {
            spec = SpecificationFactory.build(SpecificationFactory.OP.AND, typeE);
        }

        Page<RuleFileInfo> page = ruleService.page(spec, rulefilelDto.getPageNo(), rulefilelDto.getPageSize());
        return PlatformResponse.successData(page);
    }
}
